<!--
 * @Author: maqaing
 * @Date: 2021-10-24 14:49:23
 * @LastEditors: maqiang
 * @LastEditTime: 2021-10-25 17:26:41
 * @Description: 
-->
<template>
  <div>
    <!-- {{ categoryParam }} -->
    <el-button type="primary" size="mini" @click="openAddCategory"
      >新增</el-button
    >
    <el-table :data="category" stripe style="width: 100%">
      <el-table-column type="index" label="序号" width="180"> </el-table-column>
      <el-table-column prop="name" label="名称" width="180"> </el-table-column>
      <el-table-column prop="no" label="排序号"> </el-table-column>
      <el-table-column prop="description" label="描述"> </el-table-column>
      <el-table-column align="center" label="操作">
        <template v-slot='scope'>
          <el-tag type="success" @click="toEditerHander(scope.row)">编辑</el-tag>
          <el-tag type="danger" @click="toDelete(scope.row.id)">删除</el-tag>
        </template>
      </el-table-column>
    </el-table>
    <!-- 模态框 -->
    <el-dialog :title="title" :visible.sync="dialogFormVisible">
      <el-form>
        <el-form-item label="栏目名称">
          <el-input v-model="categoryParam.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="序号">
          <el-input v-model="categoryParam.no" autocomplete="off"></el-input>
        </el-form-item>
        <el-input
          type="textarea"
          :rows="2"
          placeholder="请输入内容"
          v-model="categoryParam.description"
        >
        </el-input>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="AddCategory">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
</template>
<script>
import request from "@/utils/request";
import qs from "qs";
import _ from 'lodash';

export default {
  data() {
    return {
      // 全部咨询列表
      category: [],
      //   模态框的显示与隐藏
      dialogFormVisible: false,
      //   模态框数据的保存
      categoryParam: {},
    //   模态框标题
    title:'',
    };
  },
  methods: {
    // 查询全部咨询分类
    getAllCategory() {
      request.get("/category/findAll").then((res) => {
        this.category = res.data;
        // console.log(res);
      });
    },
    // 打开新增风采模态框
    openAddCategory() {
      this.categoryParam={},
        this.title='新增栏目信息',
      this.dialogFormVisible = true;
    },
    //   新增数据
    AddCategory() {
      request
        .post("/category/saveOrUpdate", qs.stringify(this.categoryParam))
        .then(() => {
             this.$message({
            type: 'success',
            message: '更新成功!'
          });
          this.getAllCategory();
          this.dialogFormVisible = false;
          this.categoryParam = {};
        });
    },
    // 编辑模态框内容
    toEditerHander(name){
        this.title="编辑栏目信息"
        this.dialogFormVisible=true;
        // 解决浅拷贝问题
        name = _.clone(name)
        this.categoryParam=name;
    },
    // 删除数据
    toDelete(id){
           this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
              request.get('/category/deleteById',{params:{id}}).then(()=>{
            this.getAllCategory();
        });
          this.$message({
            type: 'success',
            message: '删除成功!'
          });
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });          
        });
        // console.log(id);
      
    }
  },
  created() {
    //   执行查询分类函数
    this.getAllCategory();
  },
};
</script>
<style scoped>
</style>